﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.BaseInfo;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.SMS;
using ZHDJ.WebPage;
using ZHDJ.Data.BLL;

namespace SysManage.Module.Sms
{
    public partial class SmsmoRecordEdit : PageBase
    {

        #region ViewPower
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "SmsmoRecordEdit";
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }


        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {

            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartySmsmoRecord current = DB.PartySmsmoRecords.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtMobile.Text = current.Mobile;//联系人手机号
                txtHandleBy.Text = current.HandleBy;//回复人
                txtContent.Text = current.Content;//短信留言内容
                txtCreteTime.Text = Convert.ToDateTime(current.CreateTime).ToString("yyyy-MM-dd HH:mm");//留言时间
                txtReplyTime.Text = Convert.ToDateTime(current.ReplyTime).ToString("yyyy-MM-dd HH:mm");//回复时间
                txtReplyContent.Text = current.ReplyContent;//回复内容
                cbxSmsState.Checked = current.SmsState == 1;//审核状态

            }
        }



        private void SaveJobTitle()
        {
            PartySmsmoRecord item;
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                //更新
                item = DB.PartySmsmoRecords.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
            }
            else
            {
                item = new PartySmsmoRecord();
                item.ID = StringHelper.NewID;
                DB.PartySmsmoRecords.Add(item);
            }
            item.Mobile = txtMobile.Text;//联系人手机号

            if (txtHandleBy.Text == "")
            {
                item.HandleBy = string.Empty;//回复人
            }
            else
            {
                item.HandleBy = txtHandleBy.Text;//回复人
            }

            item.Content = txtContent.Text;//短信留言内容
            item.CreateTime = Convert.ToDateTime(txtCreteTime.Text);//留言时间
            item.ReplyTime = DateTime.Now;//回复时间
            item.ReplyContent = txtReplyContent.Text;//回复内容
            item.SmsState = cbxSmsState.Checked ? 1 : 0;//回复状态

            if (IsLogin)
            {
                item.HandleBy = this.CurrentUser.LoginID;
            }
            else
            {
                item.HandleBy = string.Empty;
            }

            //同时向短信表中插入一条数据
            SMSHelper.CreateSmsSendRecord(item.Mobile, item.ReplyContent, "短信回复");

            DB.SaveChanges();
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}